package ModelLayer;
import java.util.*;


public class AddressList
{
    public ArrayList<Person> personList;
   // private Person person;
   private static AddressList instance;
    
    private AddressList()
    {
        personList = new ArrayList<Person>();
        
    }    
    
    /**
     * 
     */
    public static AddressList getInstance()
    {
        if(instance == null) {
            instance = new AddressList();
        }
        return instance;
    }

    public void addPerson(String name, String address, String city, String phone, String zip_code, long id)
    {
        personList.add(new Person(name, address, city, zip_code, phone, id));
    }
    
     public ArrayList<Person> listPersons()
    {
        return personList;
    }
    
    public boolean deletePerson(long id)
    {
        int index = 0;
        boolean found = false;
        Person person = null;
        
        while(index < personList.size() && !found)
        {
            person = personList.get(index);
            
            if(person.getId() == id)
            {
                found = true;
            }
            else
            {
                index ++;
            }
        }
        
        if(found)
        {
             personList.remove(index);
             return true;
        }
        
        else
        {
            
            return false;
        }
        
       
    }
    
    public Person findPerson(String name)
    {
        int index = 0;
        boolean found = false;
        Person person = null;
        
        while(index < personList.size() && !found)
        {
            person = personList.get(index);
            
            if(person.getName().equals(name))
            {
                found = true;
            }
            else
            {
                index ++;
            }
        }
        
        if(found)
        {
            return personList.get(index);
        }
        
        else
        {
            
            return null;
        }
    }
    
    public Person findPersonId(long id)
    {
        int index = 0;
        boolean found = false;
        Person person = null;
        
        while(index < personList.size() && !found)
        {
            person = personList.get(index);
            
            if(person.getId() == id)
            {
                found = true;
            }
            else
            {
                index ++;
            }
        }
        
        if(found)
        {
            return personList.get(index);
        }
        
        else
        {
            
            return null;
        }
    }
    public void updatePerson(String name, String address, String city, String phone, String zip_code, long id)
    {
      
     Person p = findPersonId(id);
     if(p != null)
        {
            if(name.length() > 0)
            {
                p.setName(name);   
            }
     
            if(address.length() > 0)
            {
                p.setAddress(address);   
            }
     
            if(city.length() > 0)
            {
                p.setCity(city);   
            }
     
            if(phone.length() > 0)
            {
                p.setPhone(phone);   
            }
            
            if(zip_code.length() > 0)
            {
                p.setZipCode(zip_code);
            }
      
     
         
        }
        
    }
}

   

